local condition = require "astronvim.utils.status.condition"
local hl = require "astronvim.utils.status.hl"
local status_utils = require "astronvim.utils.status.utils"

local utils = require "astronvim.utils"
local is_available = utils.is_available

local opts_d = {
  ERROR = { icon = { kind = "DiagnosticError", padding = { left = 1, right = 1 } } },
  WARN = { icon = { kind = "DiagnosticWarn", padding = { left = 1, right = 1 } } },
  INFO = { icon = { kind = "DiagnosticInfo", padding = { left = 1, right = 1 } } },
  HINT = { icon = { kind = "DiagnosticHint", padding = { left = 1, right = 1 } } },
  surround = { separator = "left", color = "diagnostics_bg", condition = condition.has_diagnostics },
  hl = hl.get_attributes "diagnostics",
  on_click = {
    name = "heirline_diagnostic",
    callback = function()
      if is_available "telescope.nvim" then
        vim.defer_fn(function() require("telescope.builtin").diagnostics() end, 100)
      end
    end,
  },
  update = { "DiagnosticChanged", "BufEnter" },
}

return {
  -- customize builtin plugins options
  {
    "goolord/alpha-nvim",
    opts = function(_, opts)
      -- customize the dashboard header
      local function button(sc, txt, keybind, keybind_opts)
        local leader = "SPC"
        local if_nil = vim.F.if_nil
        local sc_ = sc:gsub("%s", ""):gsub(leader, "<leader>")

        local opts1 = {
          position = "center",
          shortcut = sc,
          cursor = 3,
          width = 50,
          align_shortcut = "right",
          -- hl_shortcut = "Keyword",
          hl = "DashboardCenter",
          hl_shortcut = "DashboardShortcut",
        }
        if keybind then
          keybind_opts = if_nil(keybind_opts, { noremap = true, silent = true, nowait = true })
          opts1.keymap = { "n", sc_, keybind, keybind_opts }
        end

        local function on_press()
          local key = vim.api.nvim_replace_termcodes(keybind or sc_ .. "<Ignore>", true, false, true)
          vim.api.nvim_feedkeys(key, "t", false)
        end

        return {
          type = "button",
          val = txt,
          on_press = on_press,
          opts = opts1,
        }
      end
      opts.section.header.val = {
        [[          ▀████▀▄▄              ▄█ ]],
        [[            █▀    ▀▀▄▄▄▄▄    ▄▄▀▀█ ]],
        [[    ▄        █          ▀▀▀▀▄  ▄▀  ]],
        [[   ▄▀ ▀▄      ▀▄              ▀▄▀  ]],
        [[  ▄▀    █     █▀   ▄█▀▄      ▄█    ]],
        [[  ▀▄     ▀▄  █     ▀██▀     ██▄█   ]],
        [[   ▀▄    ▄▀ █   ▄██▄   ▄  ▄  ▀▀ █  ]],
        [[    █  ▄▀  █    ▀██▀    ▀▀ ▀▀  ▄▀  ]],
        [[   █   █  █      ▄▄           ▄▀   ]],
      }
      opts.config.opts.noautocmd = false
      opts.section.buttons.val = {
        button("f", " " .. " Find file", ":Telescope fd <CR>"),
        button("e", " " .. " File Explore", ":Telescope file_browser <CR>"),
        button("n", " " .. " New file", ":ene <BAR> startinsert <CR>"),
        button("o", " " .. " Old files", ":Telescope oldfiles <CR>"),
        button("w", " " .. " Find words", ":Telescope live_grep <CR>"),
        button("p", " " .. " Projects", ":Telescope projects <CR>"),
        button("c", " " .. " Config", ":e $MYVIMRC <CR>"),
        button(
          "s",
          " " .. " Restore Session",
          [[:lua require("resession").load("Last Session");vim.cmd(":ex") <cr>]]
        ),
        button("m", "󰏖 " .. " Mason", ":Mason<CR>"),
        button("l", "󰒲 " .. " Lazy", ":Lazy<CR>"),
        button("q", " " .. " Quit", ":qa<CR>"),
      }
      return opts
    end,
  },
  {
    "L3MON4D3/LuaSnip",
    opts = function()
      require("luasnip.loaders.from_vscode").lazy_load { paths = "~/Library/Application Support/Code/User/snippets/" }
    end,
  },
  {
    "windwp/nvim-autopairs",
    opts = function(_, opts)
      opts.map_bs = false
      return opts
    end,
  },
  {
    "rebelot/heirline.nvim",
    opts = function(_, opts)
      local status = require "astronvim.utils.status"
      opts.statusline[1] = status.component.mode {
        mode_text = { padding = { left = 1, right = 1 } },
        hl = { fg = "#111111", bold = true },
      }
      opts.statusline[5] =
        status.component.builder(status_utils.setup_providers(opts_d, { "ERROR", "WARN", "INFO" }, function(p_opts, p)
          local out = status_utils.build_provider(p_opts, p)
          if out then
            out.provider = "diagnostics"
            out.opts.severity = p
            if out.hl == nil then out.hl = { fg = "diag_" .. p } end
          end
          return out
        end))
    end,
  },
}
